package supporter;

import com.alibaba.fastjson.JSONArray;
import org.apache.commons.lang.StringUtils;

import java.util.List;

/**
 * @author adinlead
 */
public class SqlBuilder {
    private TableItem item;

    private int c;
    private StringBuilder sqlBuilder;
    private List<Object> paramBuilder;

    public SqlBuilder(TableItem item, String filed, Object param) {
        this.item = item;
        this.c = 0;
        this.sqlBuilder = new StringBuilder();
        this.paramBuilder = new JSONArray();
        this.eq(filed, param);
    }

    /**
     * 字段值等于
     *
     * @param key 字段
     * @param val 值
     */
    public SqlBuilder eq(String key, Object val) {
        this.sqlBuilder.append(" AND ").append(key).append(" = ?").append(++this.c);
        this.paramBuilder.add(val);
        return this;
    }
    /**
     * 字段值不等于
     *
     * @param key 字段
     * @param val 值
     */
    public SqlBuilder notEq(String key, Object val) {
        this.sqlBuilder.append(" AND ").append(key).append(" != ?").append(++this.c);
        this.paramBuilder.add(val);
        return this;
    }

    /**
     * 字段大于
     *
     * @param key 字段
     * @param val 值
     */
    public SqlBuilder gt(String key, Object val) {
        this.sqlBuilder.append(" AND ").append(key).append(" > ?").append(++this.c);
        this.paramBuilder.add(val);
        return this;
    }

    /**
     * 字段大于等于
     *
     * @param key 字段
     * @param val 值
     */
    public SqlBuilder gte(String key, Object val) {
        this.sqlBuilder.append(" AND ").append(key).append(" >= ?").append(++this.c);
        this.paramBuilder.add(val);
        return this;
    }

    /**
     * 字段小于
     *
     * @param key 字段
     * @param val 值
     */
    public SqlBuilder lt(String key, Object val) {
        this.sqlBuilder.append(" AND ").append(key).append(" < ?").append(++this.c);
        this.paramBuilder.add(val);
        return this;
    }

    /**
     * 字段小于等于
     *
     * @param key 字段
     * @param val 值
     */
    public SqlBuilder lte(String key, Object val) {
        this.sqlBuilder.append(" AND ").append(key).append(" <= ?").append(++this.c);
        this.paramBuilder.add(val);
        return this;
    }

    /**
     * 字段存在于
     *
     * @param key  字段
     * @param vals 值
     */
    public SqlBuilder in(String key, List vals) {
        this.sqlBuilder.append(" AND ").append(key).append(" IN (?").append(++this.c).append(")");
        this.paramBuilder.add(vals);
        return this;
    }

    /**
     * 字段不存在于
     *
     * @param key  字段
     * @param vals 值
     */
    public SqlBuilder notIn(String key, List vals) {
        this.sqlBuilder.append(" AND ").append(key).append(" NOT IN (?").append(++this.c).append(")");
        this.paramBuilder.add(vals);
        return this;
    }

    private String beSql(){
        return StringUtils.strip(sqlBuilder.toString().trim(),"AND");
    }

    public String countSql() {
        return this.beSql();
    }

    public String querySql() {
        return this.beSql() + item.orderBy();
    }

    public Object[] params() {
        return this.paramBuilder.toArray();
    }
}
